class WordProblemSolver {

    private static boolean isNumber(String inputString) {
        char[] chs = inputString.toCharArray();
        for (char ch : chs) {
            if (ch == '-') continue;
            if (!Character.isDigit(ch)) return false;
        }

        return true;
    }

    int solve(String inputString) {

        int results = 0;
        String[] seq = inputString.split(" ");

        if (!WordProblemSolver.isNumber(seq[2]))
            throw new IllegalArgumentException("I'm sorry, I don't understand the question!");

        results = Integer.valueOf(seq[2]);

        int index = 3;
        while (index <= seq.length - 1) {
            switch (seq[index]) {
                case "plus":
                    results += Integer.valueOf(seq[index + 1].replace("?", ""));
                    index += 2;
                    break;
                case "minus":
                    results -= Integer.valueOf(seq[index + 1].replace("?", ""));
                    index += 2;
                    break;
                case "multiplied":
                    results *= Integer.valueOf(seq[index + 2].replace("?", ""));
                    index += 3;
                    break;
                case "divided":
                    results /= Integer.valueOf(seq[index + 2].replace("?", ""));
                    index += 3;
                    break;
                default:
                    throw new IllegalArgumentException("I'm sorry, I don't understand the question!");
            }
        }

        return results;
    }
}